﻿using System;
using System.Web;
using System.Web.Hosting;

namespace Inovout.Web
{
    public class ShareingVirtualPathProvider : VirtualPathProvider
    {
        public const string VirtualMasterPagePath = "~/MasterPageDir/";
        private bool IsPathVirtual(string virtualPath)
        {
            string checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith(VirtualMasterPagePath, StringComparison.InvariantCultureIgnoreCase);
        }

        public override bool FileExists(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                ShareingVirtualFile file = (ShareingVirtualFile)GetFile(virtualPath);
                return (file == null) ? false : true;
            }
            else
            {
                return Previous.FileExists(virtualPath);
            }
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                return new ShareingVirtualFile(virtualPath);
            }
            else
            {
                return Previous.GetFile(virtualPath);
            }
        }

    }
}
